<?php

namespace App\Dao;

use Illuminate\Support\Facades\DB;

class TaskDao extends BaseDao
{
    /**
     * @desc 同步团队
     * @return void
     */
    public static function spreadIds()
    {
        DB::update("update eyes_users SET spread_ids = ',' WHERE spread_ids IS NULL");
        $lists = UsersDao::builder_query()->where('spread_id','>',0);
        if($lists->exists()){
            $lists = $lists->orderByDesc('created_at')->get()->toArray();
            foreach ($lists as $value){
                $spread_id = $value['spread_id'];
                while (true){
                    if($spread_id == 0){
                        break;
                    }
                    $user_spread_info = UsersDao::builder_query([
                        'user_id'=>$spread_id
                    ])->first()->toArray();

                    if(in_array($value['user_id'],explode(',',$user_spread_info['spread_ids']))){
                        break;
                    }

                    UsersDao::builder_query([
                        'user_id'=>$spread_id
                    ])->update(['spread_ids'=>$user_spread_info['spread_ids'].$value['user_id'].',']);
                    $spread_id = $user_spread_info['spread_id'];
                }
            }
        }
    }







    public static function spread_vip_levels()
    {
        $uesrs = UsersDao::builder_query()->where('vip_level','>=',1);
        if($uesrs->exists()){
            $users = $uesrs->get()->toArray();
            foreach ($users as $userInfo) {
                $level = $userInfo['vip_level'];
              if($userInfo['vip_level'] == 1){
                    if($userInfo['pv_nums_tuan'] >= 14970){
                        $level = 2;
                    }
                }else if($userInfo['vip_level'] == 2){
                    if($userInfo['pv_nums_tuan'] >= 58822){
                        $level = 3;
                    }
                }else if($userInfo['vip_level'] == 3){
                    $price_min = UsersDao::minPrices($userInfo['user_id']);

                    if($userInfo['pv_nums_tuan'] >= 192614 && $price_min >= 48902){
                        $level = 4;
                    }
                }else if($userInfo['vip_level'] == 4){
                    $price_min = UsersDao::minPrices($userInfo['user_id']);
                    if($userInfo['pv_nums_tuan'] >= 637722 && $price_min >= 192614){
                        $level = 5;
                    }
                }
              if($userInfo['vip_level'] != $level){
                  UsersDao::builder_query(['user_id'=>$userInfo['user_id']])->update([
                      'vip_level'=>$level
                  ]);
              }
            }

        }

    }



    public static function user_spread_dongshis(){
        $users = UsersDao::builder_query()->where('vip_level','>=',4)->where('vip_dong_level',0);
        if($users->exists()){
            $users = $users->get()->toArray();
            foreach ($users as $value){
                //查询下级尊卡会员
                $count = UsersDao::builder_query([
                    'spread_id'=>$value['user_id']
                ])->where('pv_nums_tuan','>=',998000)->count();
                if($count >= 2){
                    UsersDao::builder_query([
                        'user_id'=>$value['user_id']
                    ])->update([
                        'vip_dong_level'=>1
                    ]);
                }
            }
        }
    }

    public static function user_spread_dongshis_2()
    {
        $users = UsersDao::builder_query()
            ->where('vip_level','>=',4)
            ->where('vip_dong_level',1);
        if($users->exists()){
            $users = $users->get()->toArray();
            foreach ($users as $value){
                //查询下级初级董事会员
                $count = UsersDao::builder_query([
                    'spread_id'=>$value['user_id']
                ])->where('vip_dong_level',1)->count();
                if($count >= 2){
                    UsersDao::builder_query([
                        'user_id'=>$value['user_id']
                    ])->update([
                        'vip_dong_level'=>2
                    ]);
                }
                //高级董事
                if($count > 2){
                    UsersDao::builder_query([
                        'user_id'=>$value['user_id']
                    ])->update([
                        'vip_dong_level'=>3
                    ]);
                }
            }
        }

    }
}
